<template>
    <div>
        <el-table
        :data="tableData"
        style="width: 100%">

            <el-table-column
            label="ID"
            width="100">
            <template slot-scope="scope">
                <div slot="reference" class="name-wrapper">
                    <el-tag size="medium">{{ scope.row.id }}</el-tag>
                </div>
            </template>
            </el-table-column>

            <el-table-column
            label="UserName"
            width="100">
            <template slot-scope="scope">
                <div slot="reference" class="name-wrapper">
                    <el-tag size="medium">{{ scope.row.username }}</el-tag>
                </div>
            </template>
            </el-table-column>

            <el-table-column
            label="操作"
            width="100">
                <template slot-scope="scope">
                    <el-button
                    size="mini"
                    type="info"
                    @click="dialogVisible = true ; form.id=scope.row.id">修改权限</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="修改管理员信息" :visible.sync="dialogVisible" width="30%" center>
            <el-tree
                :data="this.data"
                show-checkbox
                default-expand-all
                node-key="id"
                ref="tree"
                highlight-current
                :props="defaultProps">
            </el-tree>

            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button @click="getCheckedKeys">完 成</el-button>

            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "role",
    data() {
      return {
        tableData: [],
        dialogVisible: false,
         form: {
          id: '',
          product_first: 'no',
          promotion_first: 'no',
          shop_first: 'no',
          census_first: 'no',
          backstage_first: 'no',
          allshop_second: 'no',
          checkshop_second: 'no',
          coupon_second: 'no',
          seckill_second: 'no',
          shopcheck_second: 'no',
          shopupdate_second: 'no',
          topsell_second: 'no',
          payrefund_second: 'no',
          topproduct_second: 'no',
          user_second: 'no',
          role_second: 'no',
          order_second: 'no',
          refund_second: 'no',
        },
        data: [{
          id: 'product_first',
          label: '商品',
          children: [{
            id: 'allshop_second',
            label: '商品列表'
          }, {
            id: 'checkshop_second',
            label: '商品审核'
          }]
        }, {
          id: 'promotion_first',
          label: '促销',
          children: [{
            id: 'coupon_second',
            label: '优惠券审核'
          }, {
            id: 'seckill_second',
            label: '秒杀活动审核'
          }]
        }, {
          id: 'shop_first',
          label: '店铺',
          children: [{
            id: 'shopcheck_second',
            label: '店铺申请审核'
          }, {
            id: 'shopupdate_second',
            label: '店铺查看权限'
          }]
        }, {
          id: 'census_first',
          label: '统计',
          children: [{
            id: 'topsell_second',
            label: '查看热销商品'
          }, {
            id: 'payrefund_second',
            label: '查看订单数量'
          }, {
            id: 'topproduct_second',
            label: '修改首页商品'
          }]
        }, {
          id: 'backstage_first',
          label: '后台',
          children: [{
            id: 'user_second',
            label: '用户/商家/管理员管理'
          }, {
            id: 'role_second',
            label: '角色权限管理'
          }, {
            id: 'order_second',
            label: '订单管理'
          }, {
            id: 'refund_second',
            label: '退款管理'
          }]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },
    mounted(){
        if(this.$cookies.get("role_second") != 'ok'){
            alert("无权限");
            this.$router.push("/adminindex")
        }else{
             this.$axios.get("/api/getAllAdmin").then(res=>{
                this.tableData = res.data
            });
        }
    },
    methods: {
      getCheckedKeys() {
        if(this.$refs.tree.getCheckedKeys().indexOf('product_first') != -1){
            this.form.product_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('promotion_first') != -1){
            this.form.promotion_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('shop_first') != -1){
            this.form.shop_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('census_first') != -1){
            this.form.census_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('backstage_first') != -1){
            this.form.backstage_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('allshop_second') != -1){
            this.form.allshop_second = 'ok'
            this.form.product_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('checkshop_second') != -1){
            this.form.checkshop_second = 'ok'
            this.form.product_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('coupon_second') != -1){
            this.form.coupon_second = 'ok'
            this.form.promotion_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('seckill_second') != -1){
            this.form.seckill_second = 'ok'
            this.form.promotion_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('shopcheck_second') != -1){
            this.form.shopcheck_second = 'ok'
            this.form.shop_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('shopupdate_second') != -1){
            this.form.shopupdate_second = 'ok'
            this.form.shop_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('topsell_second') != -1){
            this.form.topsell_second = 'ok'
            this.form.census_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('payrefund_second') != -1){
            this.form.payrefund_second = 'ok'
            this.form.census_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('topproduct_second') != -1){
            this.form.topproduct_second = 'ok'
            this.form.census_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('user_second') != -1){
            this.form.user_second = 'ok'
            this.form.backstage_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('role_second') != -1){
            this.form.role_second = 'ok'
            this.form.backstage_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('order_second') != -1){
            this.form.order_second = 'ok'
            this.form.backstage_first = 'ok'
        }if(this.$refs.tree.getCheckedKeys().indexOf('refund_second') != -1){
            this.form.refund_second = 'ok'
            this.form.backstage_first = 'ok'
        }

        this.$axios.post("/api/updateRole",this.$querystring.stringify({
            id:this.form.id,
            product_first:this.form.product_first,
            promotion_first:this.form.promotion_first,
            shop_first:this.form.shop_first,
            census_first:this.form.census_first,
            backstage_first:this.form.backstage_first,
            allshop_second:this.form.allshop_second,
            checkshop_second:this.form.checkshop_second,
            coupon_second:this.form.coupon_second,
            seckill_second:this.form.seckill_second,
            shopcheck_second:this.form.shopcheck_second,
            shopupdate_second:this.form.shopupdate_second,
            topsell_second:this.form.topsell_second,
            payrefund_second:this.form.payrefund_second,
            topproduct_second:this.form.topproduct_second,
            user_second:this.form.user_second,
            role_second:this.form.role_second,
            order_second:this.form.order_second,
            refund_second:this.form.refund_second,
            })).then(reponse=>{
            console.log(reponse);
            if(reponse.data == true){
               this.dialogVisible = false;
               alert("修改成功");
               this.$router.push("/adminindex")
            }else{
                alert("修改失败")
            }
        })
        .catch(error=>{
            console.log(error);
        })

      },
    }
}
</script>

<style scoped>

</style>